package com.zang.stackqueue;

import java.util.Stack;

/**
 * @author ZVerify
 * @since 2022/10/19 10:10
 **/
public class 删除字符串中的所有相邻重复项 {

    public String removeDuplicates(String s) {

        Stack<Character> stack = new Stack<>();
        StringBuilder stringBuffer = new StringBuilder();
        for (char c : s.toCharArray()) {
            if (!(stringBuffer.length()>0)){
                Character peek = stack.peek();
                if (peek.equals(c)){
                    stack.pop();
                }else {
                    stack.push(c);
                }
            }else {
                stack.push(c);
            }
        }
        for (Character character : stack) {
            stringBuffer.insert(0,character);
        }
        return stringBuffer.toString();
    }
}
